// pages/cart/addresslist/addresslist.js
import { axios } from '../../../utils/ajax.js' 
import { region } from '../../../data/region.js' 
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iPhoneX: false,
    data:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.model)
    if (app.globalData.model.substr(0, 8) == 'iPhone X') {
      this.setData({
        iPhoneX: true
      })
    }
    let { userid, token } = wx.getStorageSync('userinfo')
    console.log(userid, token )
    axios({
      url:'/address/addressList',
      data: {
        userid: userid,
        token: token
      }
    }).then((res)=>{
      console.log(res)
      this.setData({
        data: res.data.addressInfo?res.data.addressInfo:[]
      })
      let data = this.data.data
      if (data){
        data.forEach((value, index) => {
          value.provinceName = region.find(x => x.id == value.provinceId).name
          value.cityName = region.find(x => x.id == value.provinceId).childs.find(x => x.id == value.cityId).name
          value.districtName = region.find(x => x.id == value.provinceId).childs.find(x => x.id == value.cityId).childs.find(x => x.id == value.districtId).name
        })
        console.log(data)
        this.setData({
          data: data
        })
      }
    })
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //删除收货地址
  delete (e) {
    let addressid = e.currentTarget.dataset.addressid
    let { userid, token } = wx.getStorageSync('userinfo')
    console.log(addressid)
    wx.showModal({
      content: '确定要删除吗?',
      success:  (res)=>{
        if (res.confirm) {
          console.log('用户点击确定')
          axios({
            url:'/address/delete',
            data: {
              addressId: addressid,
              userid: userid,
              token: token
            }
          }).then((res)=>{
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            })
            console.log(res)
            let data = this.data.data
            console.log(data)
            console.log(res.data.defaultAddressId)
            data = data.filter(x => x.addressId !== addressid)
            if (res.data.defaultAddressId){
              data.find(x => x.addressId == res.data.defaultAddressId).isDefault = 1
            }
            this.setData({
              data: data
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //新增地址
  goaddaddress () {
    wx.navigateTo({
      url: 'increaseaddress/increaseaddress',
    })
  },
  //编辑收货地址
  edit (e) {
    let addressid = e.currentTarget.dataset.addressid
    console.log(addressid)
    wx.navigateTo({
      url: `increaseaddress/increaseaddress?addressid=${addressid}`,
    })
  },
  //设置默认地址
  setdefault (e) {
    let addressid = e.currentTarget.dataset.addressid
    console.log(addressid)
    let { userid, token } = wx.getStorageSync('userinfo')
    axios({
      url:'/address/setDefault',
      data: {
        addressId: addressid,
        userid: userid,
        token: token
      }
    }).then((res)=>{
      console.log(res)
      let data = this.data.data
      data.map(x => x.isDefault = 0)
      data.find(x => x.addressId == addressid).isDefault = 1
      this.setData({
        data: data
      })
    })
  }
})